
/**
 * @private
 * @description Get detailed information about
 * @constructor
 * @class
 * @version 0.1a
 */ 
function browserInfo(){
	this.browserEngine = {
		FIREFOX : 1,
		OPERA	: 2,
		IE		: 3,
		WEBKIT	: 4,
		SAFARI 	: 5,
		UNKNOWN	: 6
	};
	
	this.browser = {
		FIREFOX_2		: 1,
		FIREFOX_3 		: 2,
		SAFARI_2 		: 3,
		SAFARI_3 		: 4,
		SAFARI_4		: 5,
		OPERA_9			: 6,
		IE_6			: 7,
		IE_7			: 8,
		IE_8			: 9,
		WEBKIT			: 10,
		NETSCAPE		: 11,
		UNKNOWN			: 12,
		OPERA			: 13,
		SAFARI			: 14,
		IE				: 15
	};
	
	this.platform = {
		WINDOWS			: 1,
		MACINTEL		: 2,
		MACPPC			: 9,
		LINUX			: 3,
		SOLARIS			: 4,
		BSD				: 5,
		MOBILE			: 6,
		IPHONE			: 7,
		UNKNOWN			: 8
	};
	this.current = null;
	return this;
}

browserInfo.prototype = {
	isBrowser : function(b){
		if(this.browser[b] && this.browser[b] == this.current.BROWSER)return true;
		return false;
	},
	/**
	 * Method to get browser engine
	 * @param e Engine name (possible FIREFOX, OPERA, IE, WEBKIT, SAFARI)
	 * @return boolean
	 */	
	isEngine : function(e){
		if(this.browserEngine[e] && this.browserEngine[e] == this.current.BROWSER_ENGINE)return true;
		return false;
	},
	/**
	 * @function
	 * @description Method to parse array with browser version to associative array
	 * @param v array with browser version in format: array(major, minor, build)
	 * @return associative array ( MAJOR : major, MINOR : minor, BUILD : build)
	 */
	_setBrowserVersion : function(v){	
		return {
			MAJOR : parseInt(v[0]),
			MINOR : parseInt(v[1]),
			BUILD : parseInt(v[2])
		};
	},
	/** 
	 * get an browser engine, browser version and platform
	 * @return browser num 
	 */
	process : function(){
		var RV = {
			PLATFORM : 0,
			BROWSER : 0,
			BROWSER_ENGINE : 0,
			BROWSER_VERSION : {
				MAJOR : 0,
				MINOR : 0,
				BUILD : 0
			}
		};
		
		var s = navigator.userAgent;
		var p = navigator.platform;

		// platforms
		var is_macintel = p.indexOf('MacIntel') != -1 ? true : false;
		var is_macppc = p.indexOf('MacPPC') != -1 ? true : false;
		var is_windows = s.indexOf('Windows') > 0 ? true : false;
		var is_linux = p.indexOf('Linux') > 0 ? true : false;
		var is_bsd = p.indexOf('BSD') > 0 ? true : false;
		var is_solaris = p.indexOf('Solaris') > 0 ? true : false;
		
		// browser engine
		var is_webkit = s.indexOf('KHTML/') > 0 ? true : false;
		var is_safari = s.indexOf('Safari/') > 0 ? true : false;
		var is_firefox = s.indexOf('Firefox/') > 0 ? true : false;
		var is_netstapce = s.indexOf('Netscape/') > 0 ? true : false;
		var is_opera = s.indexOf('Opera/') > -1 ? true : false;	
		var is_ie = s.indexOf('MSIE ') > -1 ? true : false;
		
		
		// set platform
		if(is_macintel){
			RV.PLATFORM = this.platform.MACINTEL;
		}else
		if(is_macppc){
			RV.PLATFORM = this.platform.MACPPC;		
		}else
		if(is_windows){
			RV.PLATFORM = this.platform.WINDOWS;
		}else{
			RV.PLATFORM = this.platform.UNKNOWN;
		}
		
		// set browser engine
		if(is_firefox){
			RV.BROWSER_ENGINE = this.browserEngine.FIREFOX;
			//FF
			var re = new RegExp("Firefox\\\/(\\d+)\\.(\\d+)\\.(\\d+)");
			var v = re.exec(s);
			if(v != null){
				RV.BROWSER_VERSION = this._setBrowserVersion(v.slice(1));
			}
			
			// set concrete browser version
			if(RV.BROWSER_VERSION.MAJOR == 2){
				RV.BROWSER = this.browser.FIREFOX_2;
			}else
			if(RV.BROWSER_VERSION.MAJOR == 3){
				RV.BROWSER = this.browser.FIREFOX_3;			
			}
		}else
		if(is_opera){
			var re = new RegExp("Opera\\\/(\\d+)\\.(\\d+)");
			var v = re.exec(s);
			if(v != null){
				// add build because it's undefined in Opera
				v[v.length] = 0;
				RV.BROWSER_VERSION = this._setBrowserVersion(v.slice(1));
			}

			RV.BROWSER_ENGINE = this.browserEngine.OPERA;

			// set concrete browser version
			if(RV.BROWSER_VERSION.MAJOR == 9){
				RV.BROWSER = this.browser.OPERA_9;
			}else{
				RV.BROWSER = this.browser.OPERA;
			}			
		}else
		if(is_safari){
			var re = new RegExp("Version\\\/(\\d+)\\.(\\d+)");
			var v = re.exec(s);
			if(v != null){
				// add build for Safari
				v[v.length] = 0;
				RV.BROWSER_VERSION = this._setBrowserVersion(v.slice(1));
			}		
			
			RV.BROWSER_ENGINE = this.browserEngine.SAFARI;

			// set concrete browser version
			if(RV.BROWSER_VERSION.MAJOR == 2){
				RV.BROWSER = this.browser.SAFARI_2;
			}else			
			if(RV.BROWSER_VERSION.MAJOR == 3){
				RV.BROWSER = this.browser.SAFARI_3;
			}else
			if(RV.BROWSER_VERSION.MAJOR == 4){
				RV.BROWSER = this.browser.SAFARI_4;
			}else{
				RV.BROWSER = this.browser.SAFARI;
			}			
		}else
		if(is_webkit){
			RV.BROWSER_ENGINE = this.browserEngine.WEBKIT;
		}else
		if(is_ie){
			//IE
			var re = new RegExp("MSIE\\s(\\d+)\\.(\\d+);");
			var v = re.exec(s);
			if(v != null){
				// add build for IE
				v[v.length] = 0;
				RV.BROWSER_VERSION = this._setBrowserVersion(v.slice(1));
			}

			RV.BROWSER_ENGINE = this.browserEngine.IE;
			
			// set concrete browser version
			if(RV.BROWSER_VERSION.MAJOR == 6){
				RV.BROWSER = this.browser.IE_6;
			}else
			if(RV.BROWSER_VERSION.MAJOR == 7){
				RV.BROWSER = this.browser.IE_7;
			}else
			if(RV.BROWSER_VERSION.MAJOR == 8){
				RV.BROWSER = this.browser.IE_8;
			}else{
				RV.BROWSER = this.browser.IE;
			}			
		}else{
			RV.BROWSER_ENGINE = this.browserEngine.UNKNOWN;
			RV.BROWSER = this.browser.UNKNOWN;
		}

		this.current = RV;	
		return this;
	}
}
